home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 November / Macworld (1999-11).dmg / Shareware World / Info / For Developers / Smile1.6.6.sea / Smile1.6.6 / Smile ƒ / Help files / Very beginners < prev    next >
Text File  |  1999-08-18  |  6KB  |  87 lines

  1. For the very beginners
  2.  
  3.  
  4. AppleScript in a rush
  5.  
  6. AppleScript is a language intended to automatize tasks. The programs written with AppleScripts are called AppleScript scripts, or, when allowed by the context, scripts. Most often, a script will replace tenths (or millions) of manual operations by one operation such as double-clicking an icon, clicking a button, or selecting an item in a pull-down menu.
  7.  
  8. AppleScript has its own set of commands, which allows some basic manipulations of quantities such as numbers, strings, data, lists, records. This basic set can be freely extended by the mechanism of the scripting additions ("Compléments de pilotage" en français). A scripting addition (or "osax") is a file which, when installed in the proper folder, will add some functionalities to AppleScript.
  9.  
  10. Adding a functionality to AppleScript actually means adding some new terms to the AppleScript's standard terminology. These new terms are called the "dictionary" of the scripting addition. (Check the "File" menu for the item "Open dictionary…".). For instance, by installing the "Breakfast" scripting addition, you will add a "make coffee" verb to the AppleScript's terminology.
  11.  
  12. Some applications also have a dictionary, which extends still further the available terminology. AppleScript allows to send a message to some specific application : "tell application "Finder" to beep". Each application has its own dictionary, which presents what messages and what data you can send and receive to / from the application.
  13.  
  14. In applets and droplets, you fill find a tutorial about making those. Applets (for: small applications) are scripts which are launchable by double-click on their icon, while droplets are launched when files are dropped on their icon. Smile's own objects have scripts, allowing for a richer behavior. This aspect is presented below, through a simple - yet, useful - example.
  15.  
  16.  
  17. Writing your first script for Smile
  18.  
  19. • Select "New text" ("File" menu). This opens a new text (white) window.
  20.  
  21. • While pressing down the command and the option keys ( ), click in the white area of the new window. This will open a new script (colored) window.
  22.  
  23. • Type the following lines in the new window (or copy or drag the text) :
  24. ----------------------------
  25. property cookingDelay : 180 -- the cooking time, in seconds, for boiled eggs
  26. property startingTime : "" -- the time cooking started
  27.  
  28. on prepare theWindow -- sent to the script when window opens
  29.     reset(theWindow)
  30. end prepare
  31.  
  32. on reset(theWindow) -- launch timing
  33.     set startingTime to (current date)
  34.     copy return & "Start : " & (current date) & return & ¬
  35.         "Time remaining : " & cookingDelay & " seconds." to after text of theWindow
  36.     set want idle of theWindow to true -- make Smile call the "idle" handler
  37. end reset
  38.  
  39. on idle theWindow -- sent after delay = the returned value (2.0) since last time
  40.     set theElapsed to (current date) - startingTime
  41.     if theElapsed < cookingDelay then
  42.         set last paragraph of theWindow to "Time remaining : " & (cookingDelay - theElapsed) & " seconds." -- update last line
  43.         return 2.0 -- time before next call to 'idle' handler
  44.     end if
  45.     set want idle of theWindow to false -- time elapsed, suspend "idle" calls
  46.     try
  47.         say "Eggs are ready"
  48.     on error
  49.         beep 3 -- code for "Eggs are ready"
  50.     end try
  51.     set last paragraph of theWindow to "Boiled eggs ready : " & (current date) & return
  52. end idle
  53.  
  54. on DoHelp(theWindow)
  55.     try
  56.         set cookingDelay to 0 + AskUser("Cooking time (seconds) : ", "" & cookingDelay)
  57.     on error
  58.     end try
  59. end DoHelp
  60. ----------------------------
  61.  
  62. • Read the script : the "prepare" handler is called automatically by Smile when the window is opened, it just "resets" the window. The "reset" handler launches the timing. The "idle" handler is called automatically by Smile if the property "want idle" of the window is set to "true". Here, it checks whether the specified delay is elapsed. When it is, it says something or beeps.
  63. Note the way the possible failure of the "say" call (the Speech ability can be absent from the user's machine) is handled.
  64. The "DoHelp" handler is called when the user presses the "Help" key. Here, pressing the "Help" key allows to set the cooking time. The "try" in the "DoHelp" handler is intended to manage the possible "User canceled" error.
  65.  
  66. • Select "Check syntax" ("Edit" menu) (the new colored window must be the active window). Correct any possible mistake.
  67.  
  68. • Select "Save", then "Close" ("File" menu).
  69.  
  70. • Click the new white window to make it the active window, then select "Save" ("File" menu). Choose the desktop as the location for the file, name it "Egg Timer".
  71.  
  72. • Usually, you will launch the timer by double-clicking the "Egg Timer" icon. But presently it is already opened, so launch the timing manually (by script, actually). Type in the "Egg timer" window the following line :
  73. ----------------------------
  74. tell window 1 to reset(it)
  75. ----------------------------
  76.  
  77. Then press the Enter key (numeric pad). (On most machines, you can alternately type ctrl-C.)
  78.  
  79. • For testing purposes, you may want to shorten the delay. Press the "Help" key (or type -?). In the dialog, give a smaller value such as 30.
  80.  
  81. • Now, wait a few seconds, the window should soon, either say that the eggs are ready, or emit three beeps.
  82.  
  83. • Select "Save", then "Close" ("File" menu). Not only have you saved the text of the window. You have also saved its script, including the current values of its properties (the 30 seconds delay), in other terms, its settings.
  84. Later, when you need to set some alarm, double-click the document in Finder, press the "Help" key to set the delay. Smile will tell you when the alarm goes off.
  85.  
  86. ========================================================
  87.